home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / demobook / index.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  8.8 KB  |  384 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "exinterfmotif.h"
  20. #include "exbookglo.h"
  21.  
  22.  
  23. void index_add_demo(struct indexlist *ndxptr, struct icntmpltstruct *curicon)
  24. {
  25.    struct iconstruct *tmpicon;
  26.  
  27. /*
  28. printf("in index_add_demo ");
  29. printf("   keyword %s  icon %s \n", ndxptr->string, curicon->nameptr->string);
  30. */
  31.    tmpicon = ndxptr->icons;
  32.    if (tmpicon == NULL)
  33.       {
  34.       tmpicon = (struct iconstruct *)malloc(sizeof(struct iconstruct) );
  35.       ndxptr->icons = tmpicon;
  36.       }
  37.    else
  38.       {
  39.       while (tmpicon->nexticon != NULL)
  40.          {
  41.          tmpicon = tmpicon->nexticon;
  42.          }
  43.       tmpicon->nexticon = (struct iconstruct *)malloc(sizeof(struct iconstruct) );
  44.       tmpicon = tmpicon->nexticon;
  45.       }
  46.    tmpicon->nexticon = NULL;
  47.    tmpicon->iconptr = curicon;
  48. }
  49.  
  50. void index_add_group(struct indexlist *ndxptr, struct grptmpltstruct *curgrp)
  51. {
  52.    struct grpliststruct *tmpgrp;
  53.  
  54. /*
  55. printf("in index_add_group ");
  56. printf("   keyword %s  book %s \n", ndxptr->string, curgrp->nameptr->string);
  57. */
  58.    tmpgrp = ndxptr->grps;
  59.    if (tmpgrp == NULL)
  60.       {
  61.       tmpgrp = (struct grpliststruct *)malloc(sizeof(struct grpliststruct) );
  62.       ndxptr->grps = tmpgrp;
  63.       }
  64.    else 
  65.       {
  66.       while (tmpgrp->next != NULL)
  67.          {
  68.          tmpgrp = tmpgrp->next;
  69.          }
  70.       tmpgrp->next = (struct grpliststruct *)malloc(sizeof(struct grpliststruct) );
  71.       tmpgrp = tmpgrp->next;
  72.       }
  73.    tmpgrp->next = NULL;
  74.    tmpgrp->grpptr = curgrp;
  75. }
  76.  
  77. struct indexlist *(find_keyword_int(int number) )
  78. {
  79.    struct indexlist *cur;
  80.    int count;
  81.  
  82.    cur = Index;
  83.    count = 0;
  84.    while (cur!= NULL && number != count)
  85.       {
  86.       cur = cur -> next;
  87.       count++;
  88.       }
  89.  
  90. /*
  91.    if (cur != NULL)
  92.       cur->count +=1;
  93. */
  94.    return(cur);
  95. }
  96.  
  97. struct indexlist *(find_keyword(s) )
  98. char *s;
  99. {
  100.    struct indexlist *cur;
  101.    int cmp_result = 100;
  102.  
  103. /* is this string already in the index? */
  104.    cur = Index;
  105.    while (cur != NULL && (cmp_result = strcasecmp(s,cur->string)) > 0)
  106.       cur = cur->next;
  107.    if (cmp_result == 0)  /* the string is in the index */
  108.       return(cur);
  109.    else
  110.       return(NULL);
  111. }
  112.  
  113. struct indexlist *remove_keyword(iptr)
  114. struct indexlist *iptr;
  115. {
  116.    struct indexlist *tmpptr;
  117.    int width;
  118.  
  119.    iptr->count -= 1;
  120.    if (iptr->count <= 0)
  121.       {
  122.       tmpptr = Index;
  123.       if (tmpptr == iptr)
  124.          {
  125.          Index = iptr->next;
  126.          }
  127.       else
  128.          {
  129.          while (tmpptr->next != iptr)
  130.             {
  131.             tmpptr = tmpptr->next;
  132.             }
  133.          tmpptr->next = iptr->next;
  134.          }
  135.       free(iptr->string);
  136.       free(iptr);
  137.       iptr = NULL;
  138.       }
  139.    return(iptr);
  140. }
  141.  
  142. remove_keyword_icon(ndxptr, icnptr)
  143. struct indexlist *ndxptr;
  144. struct icntmpltstruct *icnptr;
  145. {
  146.    struct iconstruct *tmpicon, *previcon;
  147.    Boolean done = FALSE;
  148.  
  149.    ndxptr = remove_keyword(ndxptr);
  150.    if (ndxptr != NULL)    /* take the icon out of the keyword's list of icons */
  151.       {
  152.       previcon = NULL;
  153.       tmpicon = ndxptr->icons;
  154.       while (tmpicon != NULL && !done)
  155.          {
  156.          if (tmpicon->iconptr == icnptr)
  157.             {
  158.             done = TRUE;
  159.             if (previcon == NULL)
  160.                ndxptr->icons = tmpicon->nexticon;
  161.             else
  162.                previcon->nexticon = tmpicon->nexticon;
  163.             }
  164.          previcon = tmpicon;
  165.          tmpicon = tmpicon->nexticon;
  166.          }
  167.       if (done)
  168.          free(previcon);
  169.       }
  170. }
  171.  
  172. remove_keyword_group(ndxptr, grpptr)
  173. struct indexlist *ndxptr; 
  174. struct grptmpltstruct *grpptr;
  175. {
  176.    struct grpliststruct *tmpgrp, *prevgrp;
  177.    Boolean done = FALSE;
  178.  
  179.    ndxptr = remove_keyword(ndxptr);
  180.    if (ndxptr != NULL)
  181.       {
  182.       prevgrp = NULL;
  183.       tmpgrp = ndxptr->grps;
  184.       while (tmpgrp != NULL && !done)
  185.          { 
  186.          if (tmpgrp->grpptr == grpptr)
  187.             {
  188.             done = TRUE;
  189.             if (prevgrp == NULL)
  190.                ndxptr->grps = tmpgrp->next;
  191.             else
  192.                prevgrp->next = tmpgrp->next;
  193.             if (tmpgrp->next != NULL)
  194.                tmpgrp->next->prev = prevgrp;
  195.             }
  196.          prevgrp = tmpgrp;
  197.          tmpgrp = tmpgrp->next;
  198.          }
  199.       if (done)
  200.          free(prevgrp);
  201.       }
  202. }
  203.  
  204. /****************************************************
  205.  *
  206.  *  delete_keywords(void * ptr, int type) if type = 0, ptr = icntmpltstruct.
  207.  *  delete_keywords(void * ptr, int type) if type = 1, ptr = grptmpltstruct.
  208.  *
  209.  ***************************************************/
  210. void delete_keywords(void * ptr, int type)
  211. {
  212.    struct wordlist *wrdptr;
  213.    struct wordlist *wrd2;
  214.    struct icntmpltstruct *icnptr;
  215.    struct grptmpltstruct *grpptr;
  216.  
  217.    if (type == 0)
  218.       {
  219.       icnptr = (struct icntmpltstruct *)ptr;
  220.       wrdptr = icnptr->keywords;
  221.       }
  222.    else 
  223.       {
  224.       grpptr = (struct grptmpltstruct *)ptr;
  225.       wrdptr = grpptr->keywords;
  226.       }
  227.    while (wrdptr != NULL)
  228.       {
  229.       wrd2 = wrdptr->next;
  230.       if (type == 0)
  231.          {
  232.          remove_keyword_icon(wrdptr->indexptr,icnptr);
  233.          }
  234.       else
  235.          {
  236.          remove_keyword_group(wrdptr->indexptr,grpptr);
  237.          }
  238.       free(wrdptr);
  239.       wrdptr = wrd2;
  240.       }
  241. }
  242.  
  243. void check_index()
  244. {
  245.    struct indexlist *ndxptr;
  246.    int n;
  247.  
  248.    ndxptr = Index;
  249.    n = 0;
  250.    while (ndxptr != NULL)
  251.       {
  252.       if (ndxptr->count == 0)
  253.          remove_keyword(ndxptr);
  254.       else
  255.          {
  256.          ndxptr->num = n;
  257.          n++;
  258.          }
  259.       ndxptr = ndxptr->next;
  260.       }
  261. }
  262.  
  263. struct indexlist *add_keyword(s)
  264. char *s;
  265. {
  266.    struct indexlist *cur, *newptr, *insert;
  267.    int cmp_result = 100;
  268.    int width;
  269.  
  270. /* is this string already in the index? */
  271.    cur = Index;
  272.    insert = NULL;
  273.    while (cur != NULL && (cmp_result = strcasecmp(s,cur->string)) > 0)
  274.       {
  275.       insert = cur;
  276.       cur = cur->next;
  277.       }
  278.    if (cmp_result != 0)  /* the string is not in the index */
  279.       {
  280.       newptr = (struct indexlist *)malloc(sizeof(struct indexlist));
  281.       newptr->next = NULL;
  282.       newptr->string = (char *)malloc(strlen(s)+1);
  283.  
  284.       newptr->string = strcpy(newptr->string, s);
  285.       newptr->found_on_page = NULL;
  286.       newptr->group = NULL;
  287.       newptr->icon = NULL;
  288.       newptr->icons = NULL;
  289.       newptr->grps = NULL;
  290.       newptr->count = 1;
  291.       if (Index == NULL)
  292.          {
  293.          Index = newptr;
  294.          newptr->num = 0;
  295.          }
  296.       else
  297.          {
  298.          if (insert == NULL)
  299.             {
  300.             newptr->next = Index;
  301.             Index = newptr;
  302.             }
  303.          else
  304.             {
  305.             insert->next = newptr;
  306.             newptr->next = cur;
  307.             newptr->num = insert->num+1;
  308.             }
  309.          }
  310.       }
  311.    else /* cmp_result == 0 */
  312.       {
  313.       newptr = cur;
  314.       newptr->count += 1;
  315.       }
  316.  
  317.    return(newptr);
  318. }
  319.  
  320. void short_print_keywords()
  321. {
  322.    struct indexlist *curword;
  323.    int i;
  324.  
  325.    curword = Index;
  326.    if (curword != NULL)
  327.       printf("  INDEX: \n");
  328.    i = 0;
  329.    while (curword != NULL)
  330.       {
  331.       printf("    %d %s    %d \n", i, curword->string, curword->count );
  332.       i++;
  333.       curword = curword->next;
  334.       }
  335. printf(" \n\n");
  336. }
  337.  
  338. void print_keywords()
  339. {
  340.    struct indexlist *curword;
  341.    struct wordlist *wrdptr;
  342.    struct grpliststruct *tmpgrp;
  343.    struct iconstruct *tmpicon;
  344.    int i, count;
  345.  
  346.    curword = Index;
  347.    if (curword != NULL)
  348.       printf("  INDEX: \n");
  349.    i = 0;
  350.    while (curword != NULL)
  351.       {
  352.       printf(" %d %d    %s  %d occurences ",
  353.               i, curword->num, curword->string, curword->count);
  354.       if (curword->icon != NULL)
  355.          printf("    Icon Name \n");
  356.       if (curword->group != NULL)
  357.          printf("    Book Name \n");
  358.       tmpgrp = curword->grps;
  359.       count = 0;
  360.       printf("    Books: \n");
  361.       while (tmpgrp != NULL)
  362.          {
  363.          printf("        %s \n", tmpgrp->grpptr->nameptr->string);
  364.          count++;
  365.          tmpgrp = tmpgrp->next;
  366.          }
  367.       printf("    total %d  \n",count);
  368.       tmpicon = curword->icons;
  369.       count = 0;
  370.       printf("    Icons: \n");
  371.       while (tmpicon != NULL)
  372.          {
  373.          printf("\t\t %s \n", tmpicon->iconptr->nameptr->string);
  374.          count ++;
  375.          tmpicon = tmpicon->nexticon;
  376.          }
  377.       printf("    total %d  \n",count);
  378.       i++;
  379.       printf("\n"); 
  380.       curword = curword->next;
  381.       }
  382. printf(" done printing index \n");
  383. }
  384.